package com.framework.utils;

import org.apache.commons.lang.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Tool {
  private static Pattern linePattern = Pattern.compile("_(\\w)");
 
 /** 下划线转驼峰 */
  public static String lineToHump(String str) {
        str = str.toLowerCase();
     Matcher matcher = linePattern.matcher(str);
      StringBuffer sb = new StringBuffer();
      while (matcher.find()) {
          matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
     }
       matcher.appendTail(sb);
     return sb.toString();
 }
 
  /** 驼峰转下划线(简单写法，效率低于{@link #humpToLine2(String)}) */
 public static String humpToLine(String str) {
       return str.replaceAll("[A-Z]", "_$0").toLowerCase();
    }
 
  private static Pattern humpPattern = Pattern.compile("[A-Z]");
 
    /** 驼峰转下划线,效率比上面高 */
  public static String humpToLine2(String str) {
      Matcher matcher = humpPattern.matcher(str);
     StringBuffer sb = new StringBuffer();
     while (matcher.find()) {
          matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
      }
       matcher.appendTail(sb);
     return sb.toString();
 }
    /**
     * 下划线转换为驼峰
     *
     * @param line 下划线字符串
     * @param firstIsUpperCase 首字母是否转换为大写
     * @return
     */
    public static String underline2Camel(String line, boolean ... firstIsUpperCase) {
        String str = "";

        if(StringUtils.isBlank(line)){
            return str;
        } else {
            StringBuilder sb = new StringBuilder();
            String [] strArr;
            // 不包含下划线，且第二个参数是空的
            if(!line.contains("_") && firstIsUpperCase.length == 0){
                sb.append(line.substring(0, 1).toLowerCase()).append(line.substring(1));
                str = sb.toString();
            } else if (!line.contains("_") && firstIsUpperCase.length != 0){
                if (!firstIsUpperCase[0]) {
                    sb.append(line.substring(0, 1).toLowerCase()).append(line.substring(1));
                    str = sb.toString();
                } else {
                    sb.append(line.substring(0, 1).toUpperCase()).append(line.substring(1));
                    str = sb.toString();
                }
            } else if (line.contains("_") && firstIsUpperCase.length == 0) {
                strArr = line.split("_");
                for (String s : strArr) {
                    sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
                }
                str = sb.toString();
                str = str.substring(0, 1).toLowerCase() + str.substring(1);
            } else if (line.contains("_") && firstIsUpperCase.length != 0) {
                strArr = line.split("_");
                for (String s : strArr) {
                    sb.append(s.substring(0, 1).toUpperCase()).append(s.substring(1));
                }
                if (!firstIsUpperCase[0]) {
                    str = sb.toString();
                    str = str.substring(0, 1).toLowerCase() + str.substring(1);
                } else {
                    str = sb.toString();
                }
            }
        }
        return str;
    }
  public static void main(String[] args) {
      String lineToHump = lineToHump("f_parent_no_leader");
      System.out.println(lineToHump);// fParentNoLeader
     System.out.println(humpToLine(lineToHump));// f_parent_no_leader
      System.out.println(humpToLine2(lineToHump));// f_parent_no_leader
      String f_parent_no_leader = underline2Camel("f_parent_no_leader",true);
      System.out.println(f_parent_no_leader);
  }
}